package org.swingDemo.UI;

import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import com.esotericsoftware.tablelayout.swing.Table;
import com.formdev.flatlaf.FlatIntelliJLaf;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

public class MainFrame extends JFrame implements WindowListener {

    public static MainFrame mainFrame;

    private JTextField filePathField;
    private JButton fileSelectButton;

    private JTextField groupField;
    private JTextField artifactField;
    private JTextField versionField;
    private JComboBox<String> packagingComboBox;
    private JButton uploadButton;

    private JTextArea textArea;

    public MainFrame() {
        super("MVN本地文件上传");
        mainFrame = this;

        try {
            UIManager.setLookAndFeel(new FlatIntelliJLaf());

            initFrame();
            this.addWindowListener(this);
            this.setIconImage(new ImageIcon(MainFrame.class.getResource("/static/frame.png")).getImage());
            this.setMinimumSize(new Dimension(400, 300));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        this.setVisible(false);
        this.dispose();
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }

    private void initFrame() {
        filePathField = new JTextField();
        filePathField.setEditable(false);
        fileSelectButton = new JButton("选择文件");
        fileSelectButton.addActionListener(listener -> {
            handleFileSelect();
        });

        groupField = new JTextField();
        artifactField = new JTextField();
        versionField = new JTextField();
        packagingComboBox = new JComboBox<>();
        packagingComboBox.addItem("jar");

        uploadButton = new JButton("上传本地文件");
        uploadButton.addActionListener(listener -> {
            handleUploadFile();
        });

        textArea = new JTextArea();
        textArea.setEditable(false);

        Table pageStartTable = new Table();
        pageStartTable.addCell("文件路径:");
        pageStartTable.addCell(filePathField).expandX().fillX();
        pageStartTable.addCell(fileSelectButton);

        Table centerTable = new Table();
        centerTable.addCell("groupId:");
        centerTable.addCell(groupField).expandX().fillX();
        centerTable.row();
        centerTable.addCell("artifactId:");
        centerTable.addCell(artifactField).expandX().fillX();
        centerTable.row();
        centerTable.addCell("version:");
        centerTable.addCell(versionField).expandX().fillX();
        centerTable.row();
        centerTable.addCell("packaging:");
        centerTable.addCell(packagingComboBox).expandX().fillX();
        centerTable.row();
        centerTable.addCell(new JScrollPane(textArea)).colspan(2).expand().fill();

        this.setLayout(new BorderLayout());
        this.add(pageStartTable, BorderLayout.PAGE_START);
        this.add(centerTable, BorderLayout.CENTER);
        this.add(uploadButton, BorderLayout.PAGE_END);
    }

    private void handleFileSelect() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setCurrentDirectory(new File("F:/code/Gitee/java/mvnFileInstaller"));
        int option = fileChooser.showOpenDialog(this);
        if (option == JFileChooser.APPROVE_OPTION) {
            filePathField.setText(fileChooser.getSelectedFile().getAbsolutePath());
        }
    }

    private void handleUploadFile() {
        String filePath = filePathField.getText();
        if (filePath.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请选择待上传的文件...");
            return;
        }
        String groupId = groupField.getText();
        if (groupId.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入groupId...");
            return;
        }
        String artifactId = artifactField.getText();
        if (artifactId.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入artifactId...");
            return;
        }
        String version = versionField.getText();
        if (version.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入version...");
            return;
        }
        String packaging = packagingComboBox.getSelectedItem().toString();
        if (packaging.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请选择packaging...");
            return;
        }

        textArea.setText("");
        String mvnCommand = StrUtil.format("cmd mvn install:install-file " +
                "-Dfile={} -DgroupId={} -DartifactId={} -Dversion={} -Dpackaging={}",
                filePath, groupId, artifactId, version, packaging);
        String str = RuntimeUtil.execForStr(mvnCommand);
        System.out.println(str);
        textArea.setText(str);
    }
}
